<?php

class TabItem {
    public $Name;
    public $Num;
    public $Panel;
    public $Parent;
    public $Children;
    public $Rendered;
    public $Index;
    public $strTemplate=null;
}

class JTab {
    protected $MainTabName;
    protected $objTabs;

  
    protected $intTab;
    public function __construct($MainTabName) {
        $this->MainTabName = $MainTabName;
        $this->intTab      = 0;

    }
    Public function AddTab($Name,$Panel=null,$Parent=null){
        $this->TabName[$Name]           = $this->intTab;
        $this->objTabs[$Name]           = new Tabitem;
        $this->objTabs[$Name]->Name     = $Name;
        $this->objTabs[$Name]->Num      = $this->intTab;
        $this->objTabs[$Name]->Panel    = $Panel;
        $this->objTabs[$Name]->Rendered = false;
        if (!$Parent == null){
            if (!array_key_exists($Parent, $this->objTabs) ) {
                    echo "Error invalid parent tab";
                    exit;
            }
            $this->objTabs[$Name]->Parent       = $Parent;
            $this->objTabs[$Parent]->Children[] = $this->objTabs[$Name];
        }
        $this->intTab           = $this->intTab+ 1;
    }
    public function UnRender(){
         foreach ($this->objTabs as $tab)
            $tab->Rendered = false;
    }

    public function Render(){
        print(sprintf("<div class='tabber' id='%s'>",$this->MainTabName));
        foreach ($this->objTabs as $Key => $tab){
            if (! $tab->Rendered == true ){
                print(sprintf("<div class='tabbertab' id='%s'><h2>%s</h2>",$tab->Name,$tab->Name));
                //echo "Name = " . $Key . "  Num=" . $tab->Num . "<br>";
                $tab->Rendered= true;
                //Level 2
                if (!is_null($tab->Children)){
                    print(sprintf("<div class='tabber' id='%s-%s'>",$this->MainTabName,"1"));
                    foreach ($tab->Children as $Key => $Child){
                        print(sprintf("<div class='tabbertab' id='%s'><h2>%s</h2>",$Child->Name,$Child->Name));
                        $Child->Rendered= true;
                         //Level 3
                         if ( !is_null($Child->Children) ){
                            print(sprintf("<div class='tabber' id='%s-%s'>",$this->MainTabName,"2",$Child->Name));
                            foreach ($Child->Children as $Key => $SubChild){
                                print(sprintf("<div class='tabbertab' id='%s'><h2>%s</h2>",$SubChild->Name, $SubChild->Name));
                                $SubChild->Rendered= true;
                                if (!is_null($SubChild->Panel))
                                    $SubChild->Panel->Render();
                                print("</div>");
                            }
                            print("</div>");
                        }


                        if (!is_null($Child->Panel))
                            $Child->Panel->Render();
                        print("</div>");
                    }
                    print("</div>");
                }
                if (!is_null($tab->Panel))
                    $tab->Panel->Render();
                print("</div>");
            }
        }
        print("</div>");
        $this->UnRender();
    }
    public function RenderTest(){
       
        foreach ($this->objTabs as $Key => $tab){
            if (! $tab->Rendered == true ){       
                echo "Name = <font color='red'>" . $Key . "</font>  Num=" . $tab->Num . "<br>";
                $tab->Rendered= true;
                //2nd Level
                if (!is_null($tab->Children)){
                    foreach ($tab->Children as $Key => $Child){
                        echo "&nbsp;&nbsp;&nbsp;|+ Name = <font color='orange'>" . $Child->Name . "</font>  Num=" . $Child->Num . "<br>";
                        $Child->Rendered= true;
                            //3ed level
                            if (!is_null($Child->Children)){
                                foreach ($Child->Children as $Key => $SubChild){
                                    echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\_ Name = <font color='blue'>" . $SubChild->Name . "</font>  Num=" . $SubChild->Num . "<br>";
                                    $SubChild->Rendered= true;
                                }
                            }
                    }
                }
            }
        }
    }
    
    Public function ChangeTabById($Id,$L2Id =null,$L3Id=null){
        
        //Change Level 1
        $strJS= sprintf("document.getElementById('%s').tabber.tabShow(%s);",$this->MainTabName, $Id );
        QApplication::ExecuteJavaScript($strJS);
        //Change level 2 if any
        if ( ! is_null($L2Id)  ){
            $strTabId=sprintf("%s-%s", $this->MainTabName, "1") ;
            $strJS= sprintf("document.getElementById('%s').tabber.tabShow(%s)",$strTabId, $L2Id );
            QApplication::ExecuteJavaScript($strJS);
            if ( ! is_null($L3Id)  ){
                $strTabId=sprintf("%s-%s", $this->MainTabName, "2") ;
                $strJS= sprintf("document.getElementById('%s').tabber.tabShow(%s)",$strTabId, $L3Id );
                QApplication::ExecuteJavaScript($strJS);
            }
        }
         
    }
    /* Still bugy */
    Public function ChangeTabByName($Name){
        foreach( $this->objTabs as $tab ){
            //echo $key . "<br>";

            if ($tab->Name == $Name){
                if (!is_null($tab->Parent))
                    $strTabId=sprintf("%s-%s", $this->MainTabName,$tab->Name) ;
                else
                    $strTabId = $this->MainTabName;
                $strJS= sprintf("document.getElementById('%s').tabber.tabShow(%s)",$strTabId, $tab->Num );
                QApplication::ExecuteJavaScript($strJS);
                return;
            }
        }
    }

}

?>